package com.chzc.fm.duotin.ui.home;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import com.chzc.fm.duotin.ui.home.utils.IPlayerServer;
import com.chzc.fm.duotin.ui.home.utils.PlayerServerEventNotify;
import com.chzc.fm.duotin.utils.DuotinTask;
import com.chzc.fm.duotin.utils.FileUtils;
import com.chzc.fm.duotin.utils.HttpUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.io.File;
import java.io.IOException;


/**
 * Created by Administrator on 2016/7/3.
 */
public class Player1PlayServer extends Service implements MediaPlayer.OnPreparedListener,MediaPlayer.OnCompletionListener {

    private int i = 0;

    private MediaPlayer mediaPlayer = new MediaPlayer();

     @Override
    public void onCreate() {


        i = i + 1;
        EventBus.getDefault().register(this);
        mediaPlayer.setOnPreparedListener(this);//准备监听
        mediaPlayer.setOnCompletionListener(this);
        super.onCreate();

    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //开始或重新播放

        String url = intent.getStringExtra("mp3url");
        Log.e("url"," = "+url);
        mp3Player(url);


        return super.onStartCommand(intent, flags, startId);
    }

    @Subscribe

    public void onEvent(PlayerServerEventNotify notify) {

        if (notify.isPlaying()) {
            mediaPlayer.pause();
        } else {
            mediaPlayer.start();
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {

        return binder;
    }

    private void mp3Player(final String url) {


        //通过url播放
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpUtil.download(url, FileUtils.DIR_MP3, FileUtils.getFileNameByHashCode(url), new DuotinTask.IDownloadListener() {
                    @Override
                    public void upgradeProgress(float progress) {

                    }

                    @Override
                    public void onComplete(File file) {
                        Uri uri = Uri.fromFile(file);
                        //重置
                        mediaPlayer.reset();
                        try {
                            //设置播放路径
                            mediaPlayer.setDataSource(Player1PlayServer.this, uri);
                            //缓冲
                            mediaPlayer.prepare();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFail(String msg) {

                    }

                    @Override
                    public void start() {

                    }
                });
            }
        }).start();

}

    @Override
    public void onPrepared( MediaPlayer mp) {

        mp.start();
        Log.e("PlayServer","这里是怎么回事？");
        PlayerServerEventNotify notify = new PlayerServerEventNotify();
        notify.setPlaying(true);
        notify.setPlayPrePara(true);
        EventBus.getDefault().post(notify);

    }


    @Override
    public void onDestroy() {
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
        }
        mediaPlayer.release();
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        mp.setLooping(true);

    }

    public void seekTo(int rate){
        //手指滑动到指定位置时跳转到相应的位置
        Log.e("rate"," + "+rate+" = Duration"+mediaPlayer.getDuration());
        mediaPlayer.seekTo(rate*mediaPlayer.getDuration()/100);

    }

    public MedaiPlayMessage getMeayMeage(){

        Log.e("msg","这里执行了几次---");
        message.duration = mediaPlayer.getDuration();
        message.currentTime =mediaPlayer.getCurrentPosition();
        return message;
    }

    private  MedaiPlayMessage message = new MedaiPlayMessage();

    //定义一个数据结构用来存储播放相关的数据
    public class MedaiPlayMessage{
        public int duration;
        public int currentTime;
    }

    //传递一个本类的对象给调用者

    private IBinder binder = new Player1PlayServerBinder();

    private class Player1PlayServerBinder extends Binder implements IPlayerServer{
        @Override
        public void setProgress(int progress) {
            //这里执行了吗
            seekTo(progress);
        }

        @Override
        public MedaiPlayMessage getMessage() {
            return getMeayMeage();
        }
    }


}
